package com.brillilab.service.core.aliyun.impl;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.brillilab.common.constant.ResultEnum;
import com.brillilab.common.exceptions.BrillilabException;
import com.brillilab.common.redis.RedisCache;
import com.brillilab.common.utils.CodeUtil;
import com.brillilab.domain.vo.message.SmsSendVo;
import com.brillilab.service.constant.ServiceConstant;
import com.brillilab.service.core.aliyun.IAliYunSmsService;
import com.brillilab.service.module.aliyun.AliYunSms;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.HashMap;
import java.util.Map;

@Service
public class AliYunSmsServiceImpl implements IAliYunSmsService {

    @Autowired
    private AliYunSms aliYunSms;
    @Autowired
    private RedisCache redisCache;

    @Override
    public boolean sendVerifyCode(String mobile) {
        return sendVerifyCode(mobile,300);
    }

    @Override
    public boolean sendVerifyCode(String mobile,Integer resumeTime) {

        Integer count = null;
        String keyInc = ServiceConstant.sms_verify_code_count + mobile;
        if(aliYunSms.isSendLimitOpen()){
            //五分钟内超过5次
            count = (Integer) redisCache.getObj(keyInc);
            if (count != null && count >= aliYunSms.getSendLimit()) {
                throw new BrillilabException(ResultEnum.SEND_SMS_FREQUENT, "发送短信验证码过于频繁，请稍后再试！");
            }
        }

        // 发短信
        String code = null;
        if(aliYunSms.isTrulySend()){
            //发送短信验证码
            SmsSendVo smsSendVo = new SmsSendVo();
            smsSendVo.setMobiles(mobile);
            smsSendVo.setTemplateCode(aliYunSms.getVerifyCodeTemplate());
            code = CodeUtil.getCode(6, "1234567890");
            Map<String, Object> paramMap = new HashMap<>();
            paramMap.put("code", code);
            smsSendVo.setParamMap(paramMap);
            boolean b = this.sendSms(smsSendVo);
        }else {
            code = aliYunSms.getMockValidateCode();
        }

        //缓存验证码
        redisCache.set(ServiceConstant.sms_verify_code + mobile, code, aliYunSms.getInvalidateTime());

        if(aliYunSms.isSendLimitOpen()){
            //次数计数
            if (count == null) {
                redisCache.set(keyInc, 1, resumeTime);
            } else {
                redisCache.incr(keyInc, resumeTime);
            }
        }

        return true;
    }

    @Override
    public boolean sendEmpowerCode(String mobile, Long labId) {
        Assert.isTrue(StringUtils.isNotBlank(mobile) && labId != null, "参数缺失！");

        //五分钟内超过5次
        Integer count = null;
        String keyInc = ServiceConstant.sms_verify_code_count + mobile;
        if(aliYunSms.isSendLimitOpen()){
            //五分钟内超过5次
            count = (Integer) redisCache.getObj(keyInc);
            if (count != null && count >= aliYunSms.getSendLimit()) {
                throw new BrillilabException(ResultEnum.SEND_SMS_FREQUENT, "发送授权码过于频繁，请稍后再试！");
            }
        }

        //发送授权码
        String code = null;
        if(aliYunSms.isTrulySend()){
            SmsSendVo smsSendVo = new SmsSendVo();
            smsSendVo.setTemplateCode("SMS_146801247");
            smsSendVo.setMobiles(mobile);
            smsSendVo.setSendType(1);
            Map<String, Object> paramMap = new HashMap<>();
            code=CodeUtil.getCode(8);
            paramMap.put("code", code);
            smsSendVo.setParamMap(paramMap);
            boolean b=this.sendSms(smsSendVo);
        }else {
            code = aliYunSms.getMockValidateCode();
        }
        // 设置授权码为24小时有效
        JSONObject jsonObj = new JSONObject();
        jsonObj.put("labId", labId);
        jsonObj.put("mobile", mobile);

        redisCache.set(ServiceConstant.sms_empower_code + code, jsonObj, 86400);

        //次数计数
        if(aliYunSms.isSendLimitOpen()){
            //次数计数
            if (count == null) {
                redisCache.set(keyInc, 1, 300);
            } else {
                redisCache.incr(keyInc, 300);
            }
        }

        return true;
    }

    @Override
    public boolean sendSms(SmsSendVo smsSendVo) {
        try {
            if (smsSendVo.getSendType() == 1) {
                aliYunSms.asyncSend(smsSendVo);
                return true;
            } else {
                SendSmsResponse response = aliYunSms.syncSend(smsSendVo);
                return "OK".equals(response.getCode()) ? true : false;
            }
        } catch (Exception e) {
            throw new BrillilabException(ResultEnum.FAILURE, "短信通用发送接口异常!");
        }
    }

    @Override
    public boolean sendSmsContent(String mobile, String templateCode, Map<String, Object> paramMap) {
        //发送短信验证码
        SmsSendVo smsSendVo = new SmsSendVo();
        smsSendVo.setMobiles(mobile);
        smsSendVo.setSendType(1);
        smsSendVo.setTemplateCode(templateCode);
        smsSendVo.setParamMap(paramMap);
        // 发短信
        boolean b = this.sendSms(smsSendVo);
        return b;
    }

}
